/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Administration;

import pojo.hibernate.Institutionmaster;
import pojo.hibernate.InstitutionmasterDAO;
import pojo.hibernate.Institutionuserroles;
import pojo.hibernate.InstitutionuserroleDAO;

import utils.DevelopmentSupport;

import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author sknaqvi
 */
public class ManageInstitutionUserRole extends DevelopmentSupport {

    private List<Institutionmaster> imList = new ArrayList<Institutionmaster>();
    private InstitutionmasterDAO imDao = new InstitutionmasterDAO();
    private Institutionuserroles iur;
    private InstitutionuserroleDAO iurDao = new InstitutionuserroleDAO();
    private List<Institutionuserroles> iurList = new ArrayList<Institutionuserroles>();

    private String message;
    Integer IurId;

    public void setiur(Institutionuserroles iur) {
        this.iur = iur;
    }

    public Institutionuserroles getiur() {
        return this.iur;
    }

   public void setimList(List<Institutionmaster> imList) {
        this.imList = imList;
    }

    public List<Institutionmaster>  getimList() {
        return this.imList;
    }

    public void setIurId(Integer IurId) {
        this.IurId = IurId;
    }

    public Integer getIurId() {
        return this.IurId;
    }


    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return this.message;
    }

    public void setiurList(List<Institutionuserroles> iurList) {
        this.iurList = iurList;
    }

    public List<Institutionuserroles> getiurList() {
        return this.iurList;
    }

    @Override
    public String execute() throws Exception {
        try {
            imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));

            return SUCCESS;
        } catch (Exception e) {
            message = "Exception in -> ManageInstitutionRoleAxn"  + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }


    public String Save() throws Exception {
        try {
            //If part saves record for the first time; else parts is for record update            
            if(iur.getIurId() == null) {
               iurDao.save(iur);
            } else {          
                Institutionuserroles iur2 = iurDao.findByIURId(iur.getIurId());
                iur2 = iur;
                iurDao.update(iur2);
                // Prepare list for showing Institution User Role
                iurList = iurDao.findByInstitutionId(iur.getInstitutionmaster().getImId());

          }           
           imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
           iur = null;
           return SUCCESS;

        } catch (Exception e) {
            if (e.getCause().toString().contains("UNIQUE_IUR_Name_IUR_IM_ID")) 
                message = "The role you are trying to create already exists";          
            else
                message = "Exception in Save method -> ManageInstitutionRoleAxn " + e.getMessage() + " Reported Cause is: " + e.getCause();
            imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
            return ERROR;
        }
    }

    public String FetchIUR() throws Exception {
        try {

           iurList = iurDao.findByInstitutionId(iur.getInstitutionmaster().getImId());
           imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
            return SUCCESS;

        } catch (Exception e) {
            message = "Exception in Save method -> ManageInstitutionRoleAxn " + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

    public String DeleteIUR() throws Exception {
        try {

           iur = iurDao.findByIURId(getIurId());
           imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
           iurDao.delete(iur);           
           iurList = iurDao.findByInstitutionId(iur.getInstitutionmaster().getImId());
            return SUCCESS;

        } catch (Exception e) {
            if (e.getCause().toString().contains("FK_ERPMUR_IUR_ID"))
                message = "This role is already assigned to users. Cannot delete";
            else
                message = "Exception in Delete method -> ManageInstitutionRoleAxn " + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

    public String EditIUR() throws Exception {
        try {

           iur = iurDao.findByIURId(getIurId());
           imList = imDao.findInstForUser(Integer.valueOf(getSession().getAttribute("userid").toString()));
           return SUCCESS;

        } catch (Exception e) {
           // if (e.getCause().toString().contains("FK_ERPMUR_IUR_ID"))
           //     message = "This role is already assigned to users. Cannot delete";
           // else
                message = "Exception in Delete method -> ManageInstitutionRoleAxn " + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }


    public String AddEditIURP() throws Exception {
        try {

           return SUCCESS;

        } catch (Exception e) {
                message = "Exception in Delete method -> ManageInstitutionRoleAxn " + e.getMessage() + " Reported Cause is: " + e.getCause();
            return ERROR;
        }
    }

}
